<template>
  <div class="app">
    <div class="main">
      <div class="info">
        <div class="top">
          <img :src="logoimg" alt="富城旅游logo" />
        </div>
        <el-input
          prefix-icon="el-icon-user-solid"
          placeholder="账号"
          v-model="username"
          clearable
        >
        </el-input>
        <el-input
          prefix-icon="el-icon-s-promotion"
          placeholder="密码"
          v-model="password"
          show-password
        >
        </el-input>
        <el-button type="primary" @click="login()">立即登录</el-button>
        <el-divider>or</el-divider>
        <el-button type="success" @click="register()">点击注册</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { ElMessage } from "element-plus";
export default {
  name: "Login",
  data() {
    return {
      logoimg: require("@/assets/logo.png"),
      username: "",
      password: "",
    };
  },
  methods: {
    login() {
      if (
        this.username == "" ||
        this.password == "" ||
        this.username == null ||
        this.password == null
      ) {
        ElMessage.error("账号或密码不能为空");
      } else {
        this.$http
          .post("/api/login", {
            username: this.username,
            password: this.password,
          })
          .then((res) => {
            // console.log(res.data.msg);
            if (res.data.msg == "success") {
              ElMessage.success("登录成功");
              let name = this.username;
              //将数据存入session
              sessionStorage.setItem("user_name", JSON.stringify(name));
              //从session中拿取数据
              // let myName = JSON.parse(sessionStorage.getItem("user_name"));
              this.$router.push("/main");
            } else if (res.data.msg == "error") {
              ElMessage.error("密码错误,再检查一下吧");
            } else {
              ElMessage.error("该账号还未注册");
            }
          });
      }
    },
    register(){
        this.$router.push("/register");
    }
  },
};
</script>

<style lang="scss" scoped>
.app {
  width: 100%;
  height: 100%;
  background: #f1f1f1;
  display: flex;
  justify-content: center;
  overflow: hidden;
  & > .main {
    width: 980px;
    height: 480px;
    background: #fff;
    display: flex;
    justify-content: center;
    border-bottom-right-radius: 10px;
    border-bottom-left-radius: 10px;
    box-shadow: 0 10px 23px 0 rgba(134, 134, 150, 0.1);
    & > .info {
      width: 30%;
      height: 100%;
      & > .top {
        width: 100%;
        height: 60px;
        margin-top: 20px;
        margin-bottom: 20px;
        & > img {
          height: 100%;
        }
      }
      & > .el-input {
        margin-bottom: 20px;
      }
      & > .el-button {
        width: 100%;
        //   margin-top: 20px;
      }
    }
  }
}
</style>